//
//  AFNTool.m
//  AFN
//
//  Created by 李占鹏 on 16/6/28.
//  Copyright © 2016年 Batac_Lee. All rights reserved.
//

#import "AFNTool.h"
#import <AFNetworking.h>

@interface AFNTool()

@end




@implementation AFNTool


-(void)request:(AFNRequestType)type
     urlString:(NSString *)urlString
    parameters:(NSDictionary *)parameters
      finished:(void (^)(id result, NSError * error)) finished{
    
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
   
    AFNRequestType requestType = AFNRequestGET;
    if (requestType == type) {
        //网络请求是GET
        [manager GET:urlString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            finished(responseObject, nil);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            finished(nil, error);
        }];
    }else{
        //网络请求是POST
        [manager POST:urlString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            finished(responseObject, nil);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            finished(nil, error);
        }];
    }
    
    
    
    
    
    
    
}









static AFNTool * _tool;//设置一个全局变量,用来存放创建的唯一实例对象
//使用alloc方式创建一个实例
+(instancetype)allocWithZone:(struct _NSZone *)zone{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _tool = [super allocWithZone:zone];
    });
    return _tool;
}
//使用share的方式获取一个实例
+(instancetype)shareInstance{
    if (_tool == nil) {
        _tool = [[AFNTool alloc] init];
    }
    return _tool;
}
//使用cope的方式获取一个实例
-(id)copyWithZone:(NSZone *)zone{
    return _tool;
}
-(id)mutableCopyWithZone:(NSZone *)zone{
    return _tool;
}
@end
